package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {
  @Autowired
  private SetmealService setmealService;
  
  /**
   * 分页查询套餐
   */
  @GetMapping("/page")
  public Result<PageResult> page(SetmealPageQueryDTO queryDTO) {
    log.info("套餐分页查询：{}", queryDTO);
    PageResult page = setmealService.page(queryDTO);
    return Result.success(page);
  }
  
  /**
   * 根据id查询套餐，用于修改回显
   */
  @GetMapping("/{id}")
  public Result<SetmealVO> findById(@PathVariable Long id) {
    SetmealVO setmealVO = setmealService.findById(id);
    return Result.success(setmealVO);
  }
  
  
  /**
   * 新增套餐
   * @param setmealDTO
   */
  @PostMapping
  @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")  // 清理缓存数据
  public Result add(@RequestBody SetmealDTO setmealDTO) {
    log.info("新增套餐: {}", setmealDTO);
    setmealService.add(setmealDTO);
    return Result.success();
  }
  
  /**
   * 修改套餐
   *  - 全部清理缓存
   */
  @PutMapping
  @CacheEvict(cacheNames = "setmealCache", allEntries = true)
  public Result update(@RequestBody SetmealDTO setmealDTO) {
    setmealService.update(setmealDTO);
    return Result.success();
  }
  
  
  /**
   * 批量删除套餐
   *  - 全部清理缓存
   */
  @DeleteMapping
  @CacheEvict(cacheNames = "setmealCache", allEntries = true)
  public Result delete(@RequestParam List<Long> ids) {
    log.info("批量删除的套餐ids：{}", ids);
    setmealService.batchDel(ids);
    return Result.success();
  }
  
  
  /**
   * 起售停售套餐
   *  - 全部清理缓存
   */
  @PostMapping("/status/{status}")
  @CacheEvict(cacheNames = "setmealCache", allEntries = true)
  public Result changeStatus(@PathVariable Integer status, @RequestParam Long id) {
    System.out.println(status +" 》》》》"+ id);
    setmealService.changeStatus(status, id);
    return Result.success();
  }
  
  
}
